package com.lmf.rabbitmq.utils;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 创建一个RabbitMQ的连接工具类：
 */
public class RabbitMqUtils {

    //得到一个channel信道
    public static Channel getChannel() throws IOException, TimeoutException {
        //创建一个工厂类
        ConnectionFactory factory = new ConnectionFactory();
        //指定要连接的IP地址，rabbitmq账号，密码
        factory.setHost("192.168.120.128");
        factory.setUsername("admin");
        factory.setPassword("123456");

        //channel 实现了自动 close 接口 自动关闭 不需要显示关闭
        //创建连接
        Connection connection = factory.newConnection();
        //获取信道
        Channel channel = connection.createChannel();
        return channel;
    }
}
